package com.shwread.android.ui.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.shwread.android.qysw.activity.ReadingBookActivity;
import com.shwread.android.qysw10000204.R;

import org.geometerplus.zlibrary.text.view.ZLTextView;
import org.geometerplus.zlibrary.text.view.ZLTextView.PagePositionPercent;

import logic.constants.DefaultConsts;
import logic.util.logutil.LogUtil;


public class PopWindowJump
        extends
        ReadingPopWindow
        implements
        OnSeekBarChangeListener,
        OnClickListener
{
	
	private TextView                mPageText;
	private Button                  mLeftBtn;
	private Button                  mRightBtn;
	private SeekBar                 mNavigationBar;
	
	public PopWindowJump(Context context) {
		View contentView = LayoutInflater.from(context).inflate(R.layout.menu_pop_jump, null);
		init(context, contentView);
	}
	
	@Override
	protected void addListener() {
		mNavigationBar.setOnSeekBarChangeListener(this);
		mLeftBtn.setOnClickListener(this);
		mRightBtn.setOnClickListener(this);
	}
	
	@Override
	protected void initView(View parentView) {
		mPageText = (TextView) parentView.findViewById(R.id.menu_pop_jump_value_tv);
		mLeftBtn = (Button) parentView.findViewById(R.id.menu_pop_jump_left_btn);
		mRightBtn = (Button) parentView.findViewById(R.id.menu_pop_jump_right_btn);
		mNavigationBar = (SeekBar) parentView.findViewById(R.id.menu_pop_jump_sb);
		setupNavigation();
	}
	
	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		if (fromUser)
		{
			final int page = progress;
			final int pagesNumber = seekBar.getMax();
			gotoPage(page);
			mPageText.setText(makeProgressText(page, pagesNumber));
			//#debug
			base.tina.core.log.LogPrinter.d("FangW", "init current:" + page + "! init total" + pagesNumber);
		}
	}
	
	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		
	}
	
	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		
	}
	
	private void gotoPage(int page) {
		final ZLTextView view = ((ReadingBookActivity) context).myFBReaderApp.getTextView();
		if (page == 1)
		{
			view.gotoHome();
		}
		else
		{
			view.gotoPage(page - 1);
		}
		((ReadingBookActivity) context).myFBReaderApp.getViewWidget().reset();
		((ReadingBookActivity) context).myFBReaderApp.getViewWidget().repaint();
	}
	
	public void setupNavigation() {
		
		final ZLTextView textView = ((ReadingBookActivity) context).myFBReaderApp.getTextView();
		final ZLTextView.PagePosition pagePosition = textView.pagePosition();
		if (mNavigationBar.getMax() != PagePositionPercent.CountPage || mNavigationBar.getProgress() != PagePositionPercent.CurrPage)
		{
			LogUtil.d(DefaultConsts.TAG, "--CountPage:"+PagePositionPercent.CountPage);
			LogUtil.d(DefaultConsts.TAG, "--CurrPage:"+PagePositionPercent.CurrPage);
			mNavigationBar.setMax(PagePositionPercent.CountPage );
			mNavigationBar.setProgress(PagePositionPercent.CurrPage);
			mPageText.setText(makeProgressText(PagePositionPercent.CurrPage, PagePositionPercent.CountPage));
			
			//#debug
			base.tina.core.log.LogPrinter.d("FangW", "current:" + PagePositionPercent.CurrPage + "! total" + PagePositionPercent.CountPage);
		}
	}
	
	public void reset(){
		mNavigationBar.setMax(PagePositionPercent.CountPage);
		mNavigationBar.setProgress(PagePositionPercent.CurrPage);
		mPageText.setText(makeProgressText(PagePositionPercent.CurrPage, PagePositionPercent.CountPage));
	}
	
	private String makeProgressText(int page, int pagesNumber) {
		final StringBuilder builder = new StringBuilder();
		builder.append(page);
		builder.append("/");
		builder.append(pagesNumber);
		return builder.toString();
	}
	
	@Override
	public void onClick(View v) {
		int page = mNavigationBar.getProgress();
		final int pagesNumber = mNavigationBar.getMax();
		switch (v.getId()) {
			case R.id.menu_pop_jump_left_btn:
				//page = page - 10 <= 1 ? 1 : page - 10;
				page = 1;
				break;
			case R.id.menu_pop_jump_right_btn:
				//page = page + 10 >= pagesNumber ? pagesNumber : page + 10;
				page = pagesNumber;
				break;
		}
		
		gotoPage(page);
		mPageText.setText(makeProgressText(page, pagesNumber));
		mNavigationBar.setProgress(page);
	}
}
